# https://passport.csdn.net/login?code=public
from openpyxl import Workbook
from openpyxl import load_workbook
from openpyxl.writer.excel import ExcelWriter
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common import exceptions as ex


url = "https://passport.csdn.net/login?code=public"
driver = webdriver.Chrome(executable_path=r'C:\Users\pinkshaw\AppData\Local\Google\Chrome\Application\chromedriver.exe')
driver.get(url)
mainHandle = driver.current_window_handle
driver.implicitly_wait(5)
driver.find_element_by_link_text("账号密码登录").click()
usernameInput =driver.find_element_by_xpath('//*[@id="all"]')
passwordInput=driver.find_element_by_xpath('//*[@id="password-number"]')
commitBtn=driver.find_element_by_xpath('//button[@class="btn btn-primary__disabled"]')
index =load_workbook(r"account.xlsx")
sheetnames =index.sheetnames
sheet = index[sheetnames[0]]
i = 1
while True:
    i+=1
    avaliable =sheet.cell(row=i,column=1).value
    if not avaliable:
        break
    username =sheet.cell(row=i,column=2).value
    password =sheet.cell(row=i,column=3).value
    antiception = sheet.cell(row=i,column=4).value
    usernameInput.clear()
    if(username):
        usernameInput.send_keys(username)
    passwordInput.clear()
    if(password):
        passwordInput.send_keys(password)
    sleep(1)
    commitBtn.click()
    sleep(2)
    curl =driver.current_url
    if curl == url:
        message = driver.find_element_by_xpath('//*[@id="js_err_dom"]').text
    else:
        message ="success login!!"
        # if curl == "csdn.net":
        #     quitBtn = driver.find_element_by_xpath('//*[@id="csdn-toolbar"]/div/div/div[3]/div[3]/div/a[13]')
        #     quitBtn.click()
        driver.delete_all_cookies()
        driver.get(url)
        driver.find_element_by_link_text("账号密码登录").click()
        usernameInput =driver.find_element_by_xpath('//*[@id="all"]')
        passwordInput=driver.find_element_by_xpath('//*[@id="password-number"]')
        commitBtn=driver.find_element_by_xpath('//button[@class="btn btn-primary__disabled"]')
    if not (message):
        message ="获取信息失败"
    print("提示信息："+message)
    sheet['E'+str(i)] = message 
    if not message == antiception:
        print("发生问题：得出结果与预期不一致")
    else:
        print("测试正常：得出结果与预期一致")
    index.save(r"account.xlsx")
    sleep(1)
print("页面测试完毕，结果已成功保存！")
driver.quit()
#获取错误信息

